#pragma once
#include "log.hpp"
#include <iostream>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "protocol.hpp"

void getrequst(Request& req){
    std::string getinfo;
    std::getline(std::cin,getinfo);
    //"1+2";"11+2"
    int status = 0; //0 left 1 oper 2right
    int cnt =0; //记录步数
    std::string left,right;
    char oper;
    while (cnt < getinfo.size())
    {   
        if(status == 0 && isdigit(getinfo[cnt])){
            left.push_back(getinfo[cnt++]);
        }else if(status == 0 && (getinfo[cnt] == '+' ||getinfo[cnt] == '-'||getinfo[cnt] == '*'||getinfo[cnt] == '/'||getinfo[cnt] == '%')){
            status = 2;
            oper = getinfo[cnt++];
        }else if(status ==2){
            right.push_back(getinfo[cnt++]);
        }
    }
    req._left = atoi(left.c_str());
    req._right = atoi(right.c_str());
    req._oper = oper;
    
}

class tcpClient{
public:
    tcpClient(const std::string& serverip,const int16_t& serverport) :_serverip(serverip),_serverport(serverport),_sockfd(-1)
    {}
    
    void initClient(){
        _sockfd = socket(AF_INET,SOCK_STREAM,0);
        if(_sockfd == -1){
            logmessage(FATAL,"socket filed!");
            exit(1);
        }else logmessage(NORMAL,"socket success");
    }

    void start(){
        sockaddr_in remote;
        socklen_t len = sizeof remote;
        memset(&remote,0,len);
        remote.sin_family = AF_INET;
        remote.sin_addr.s_addr = inet_addr(_serverip.c_str());
        remote.sin_port = htons(_serverport);
        if(connect(_sockfd,(sockaddr*)&remote,len) != 0){
            logmessage(FATAL,"connect filed!");
            exit(1);
        }
        logmessage(NORMAL,"connect succuss!");
        while(true){
            //测试
            //生成请求request
            Request req;
            getrequst(req);
            //序列化
            std::string serz;
            req.serialization(&serz);
            //添加报头
            std::string sendinfo = addheader(serz);
            //发送
            ssize_t r = send(_sockfd,sendinfo.c_str(),sendinfo.size(),0);
            if(r == -1){
                logmessage(ERROR,"write filed");
            }else{
                //std::cout<<"fa shong cg"<<std::endl;
                std::cout<<"发送报文成功"<<sendinfo<<std::endl;
            }
        #define MAXBUFFER 1024
            char buffer[MAXBUFFER];
            ssize_t s = recv(_sockfd,buffer,MAXBUFFER-1,0);
            if(s >0){
                buffer[s] = 0;
                std::cout<<buffer<<std::endl;
                std::string re = deleteheader(buffer);
                Response res;
                res.unserialization(re);
                std::cout<<res._exitcode<<res._result<<std::endl;
            }else if(s == 0){
                logmessage(ERROR,"read filed,server close");
                break;
            }
        }
    }


    ~tcpClient(){
        close(_sockfd);
    }

private:
    std::string _serverip;
    uint16_t _serverport;
    int _sockfd;
};